package com.asset.dispoal.file.agreementCaseData.service.commom;

import com.alibaba.fastjson2.JSONObject;
import com.asset.dispoal.business.bsAgreement.domain.db.mapper.AgreementMapper;
import com.asset.dispoal.business.bsEvidenceRecognition.domain.redis.dto.RedisAddressDTO;
import com.asset.dispoal.file.agreementCaseData.dto.AddressGetCodeRequest;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.core.entity.RequestModel;
import tech.mhuang.pacebox.springboot.core.rest.SingleRestTemplate;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.redis.commands.IRedisExtCommands;

import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class CaseAddressGetCodeCommand implements BaseCommand<AddressGetCodeRequest, Result> {

    @Autowired
    AgreementMapper agreementMapper;

    @Autowired
    Environment environment;


    @Autowired
    IRedisExtCommands redisExtCommands;

    @Autowired
    RedisProperties redisProperties;

    @Autowired
    SingleRestTemplate singleRestTemplate;

    @SneakyThrows
    @Override
    public Result execute(AddressGetCodeRequest request) {
        JSONObject jsonObject = new JSONObject();
        Integer checked = agreementMapper.checkInstBatch(request.getAssetInstBatchId());
        if (checked == 1) {
            return Result.faild("正在运行中、请稍后再试");
        }
        List<Map<String, String>> list = agreementMapper.queryMapByAssetInstIdAndCheckAddress(request.getAssetInstBatchId());

        if (CollectionUtil.isEmpty(list)) {
            return Result.faild("没有可读取的地址");
        }
        list.forEach(map -> jsonObject.put(map.get("id"), map.get("address")));

        agreementMapper.updateAddressProcessStateByInstBatchId(request.getAssetInstBatchId());
        remoteCall(request.getAssetInstBatchId(), jsonObject);
        return Result.ok();
    }

    private void remoteCall(String id, JSONObject jsonObject) {
        redisExtCommands.hset(6, "address_before", id, jsonObject);
        RedisAddressDTO dto = new RedisAddressDTO();
        dto.setDb("6");
        dto.setId("1");
        dto.setKey(id);
        dto.setHost(redisProperties.getHost());
        dto.setPort(redisProperties.getPort());
        dto.setPasswd(redisProperties.getPassword());
        dto.setAddressData("address_before");
        dto.setErrorKey("address_err_key");
        dto.setNotifyKey("address_notify_key");
        dto.setServerUrl(environment.getProperty("evidence.address.notify_url"));
        RequestModel<String> requestModel = new RequestModel<String>() {
        };
        requestModel.setUrl(environment.getProperty("evidence.address.send_url"));
        requestModel.setMethod(HttpMethod.POST);
        requestModel.setParams(dto);
        singleRestTemplate.request(requestModel);
    }
}